--[[
-- Author:      feilong
-- DateTime:    2020-10-10 17:19:33
-- Description: 节点的服务管理
    1更方便的取得节点中各服务的信息，
    2处理节点间的消息
    3开启Gate端口监听消息，client消息由watchdog处理
--]]


local skynet = require "skynet"


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor()
    self:register()
end

--注册本服务里的消息
function MessageHandler:register()
    --self.message_dispatch = g.dispatch
    self:callback('cluster_broadcast', self.clusterBroadcast) --给所有集群节点发消息
    self:callback('cluster_broadcast_res', self.clusterBroadcastRes) --收到集群广播
    --self.message_dispatch:register('get_server', handler(self, self.getServer)) --取得指定类型的服务器节点
    self:callback('send__user', self.sendUser) --给用户发消息
    self:callback('kick_user', self.kickUser) --踢人
end


---------------------------------------------------------
-- CMD
---------------------------------------------------------
function MessageHandler:onStart()
    g.log:debug("manger service start!")
    --开启网络
    local watchdog = g.newService("watchdog.service")
    local gate = g.oldService("gate")
    local server_info = g.getServerInfo()
    local conf = {
        port = server_info.client_port,
        maxclient = server_info.maxclient,
        nodelay = true,
        watchdog = watchdog --消息将统一转到watchdog
    }
    skynet.name(".gate", gate)
    g.call(gate, "open", conf)
    g.log:debug("open gate port--> ", server_info.client_port)

end

--发消息到watchdog
function MessageHandler:sendWatchdog(cmd, msg_name, msg_data, ...)
    g.send(".watchdog.service", cmd, msg_name, msg_data, ...)
end

function MessageHandler:sendUser(msg_name, msg_data, ...)
    self:sendWatchdog("send_user", msg_name, msg_data, ...)
end

function MessageHandler:kickUser(aid)
    self:sendWatchdog("kick_user", aid)
end

function MessageHandler:clusterBroadcast(msg, msg_name)
    g.log:debug("manger clusterBroadcastReq", msg, msg_name)

    return true
end

function MessageHandler:clusterBroadcastRes(from_server, msg)
    g.log:debug("manger clusterBroadcastRes")
    return true
end

return MessageHandler